﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Brin.Concurrent;

namespace Brin.Concurrent.Test
{
    class MulInt
    {
        /// <summary>
        /// Holds the accumulated multiplied value which can be 
        /// accessed by multiple threads.
        /// This variable must be accessed in a thread safe way
        /// </summary>
        int _total = 0;

        public MulInt(int initial)
        {
            _total = initial;
        }

        /// <summary>
        /// Retruns the total value
        /// </summary>
        public int Total
        {
            get { return _total; }

        }

        /// <summary>
        /// Multiplies the running total with the specified factor, 
        /// in a thread safe way
        /// </summary>
        public void MultiplyTotal(int factor)
        {
            //This method multiplies _total with factor, 
            //saves the product in _total and returns the product atomically 
            InterlockedEx.Mul(ref _total, factor);
        }
    }
}
